clear

set more off
set mem 100m


local EVENT  "decevent" /*compevent or decevent*/
local thintrade    = 0 /*0: Do not deal with thin trading issues, 1 deal with  them using trade-to-trade returns*/
local controlevents = 1  /*Whether to include dummies for the corporate events occuring during the relevant periods*/
local controlindustry = 1 /*Whether to include a industrial index in the baseline model (improves the fit significanlty*/
local robust = 1 /*Use longest estimation window (There are 1-7 for the non-thin trade case only)*/
local estwindow 	= 180 /*Calender days*/
local evtwindow 	= 30 /*Calender days: this does not define the length of the window for the actual estimation, just for calling the file. I'm using a window of 10 in line 42*/


*use Data_Event_Study_final_thin`thintrade'_event`controlevents'_ind`controlindustry'_`EVENT', clear
use Data_Event_Study_final_thin`thintrade'_est`estwindow'_evt`evtwindow'_control`controlevents'_ind`controlindustry'_`EVENT', replace

///DROP SAMPLES WITH SHORT ESTIMATION WINDOW
egen tmp1 = min(event_time), by(panel_id)
drop if tmp1>-60 
drop tmp*
drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/

if "`EVENT'"=="majorinitiative1" | "`EVENT'"=="majorinitiative1summit" {
	drop if ticker=="GND" & eventyear==1996
}



if `thintrade'== 0 {
	local thin = ""
}
else if `thintrade'== 1 {
	local thin = "tt"
}


keep if event_time>=-123

keep panel_id estimation_window event_window event_time AbnormalReturns`thin' AbnormalReturns`thin'd R Rd eventyear
drop if (estimation_window==. & event_window==.) | event_time==.


******W/ DIVIDENDS*******
sort panel_id AbnormalReturns`thin'd

//by panel_id: gen K = [_n]

egen K = rank(AbnormalReturns`thin'd), by(panel_id)
egen maxKfirm    = max(K), by(panel_id)
egen maxKevent = max(K), by(eventyear)
egen maxKall		= max(K)

gen K_event = int(K*maxKevent/maxKfirm)     /*Here I am following  Campbell-Cowan-Salotti*/
egen K_all = mean(K*maxKall/maxKfirm), by(event_time)
replace K_all = int(K_all)

//egen meanK_event = mean(K_event), by(eventyear)

if `thintrade'==0 {
	gen isatrade = 0 if Rd~=.
	replace isatrade = 1 if Rd~=0
}
else if `thintrade'==1 {
	gen isatrade=panel_id~=.
}



collapse K_event K_all event_window estimation_window (count) nfirms=panel_id (sum) isatrade, by(eventyear event_time)

if `thintrade'==0 {
	replace nfirms = isatrade
	egen nfirms_all = sum(isatrade), by(event_time)
}
else if `thintrade'== 1 {
	egen nfirms_all = sum(nfirms), by(event_time)
}

/*Counting the median number of firms in the event window as a minimum threshold to count the average (this is liberal, rank test should be large N)*/

egen tmp = median(nfirms) if event_window==1, by(eventyear)
egen tmp2 = median(nfirms_all) if event_window==1
egen mednfirms = min(tmp), by(eventyear)
egen mednfirms_all = min(tmp2)
drop tmp tmp2

egen onepereventday = tag(event_time)

egen avK_event = mean(K_event) if nfirms>=mednfirms | event_window==1, by(eventyear)
egen avK_all		= mean(K_all) if onepereventday==1 & (nfirms_all>=mednfirms_all | event_window==1)

gen meanK_event = K_event - avK_event
gen meanK_all		= K_all - avK_all


egen sdK_event = sd(K_event) if nfirms>=mednfirms | event_window==1, by(eventyear)
egen sdK_all		= sd(K_all) if onepereventday==1 & (nfirms_all>=mednfirms_all | event_window==1)


keep if event_window==1

*RANK TEST STATISTICS

bysort eventyear: gen cumKevent = sum(meanK_event)
bysort eventyear: gen cumKall = sum(meanK_all)

by eventyear: gen T = _n if event_window==1

gen sdcumKevent = sdK_event*sqrt(T)
gen sdcumKall = sdK_all*sqrt(T)

gen ciu_event = cumKevent + sdcumKevent*1.64
gen cil_event = cumKevent - sdcumKevent*1.64

gen ciu_all = cumKall + sdcumKall*1.64
gen cil_all = cumKall - sdcumKall*1.64

gen rank_all = meanK_all/sdK_all
gen rank_event = meanK_event/sdK_event
gen pvalue_event = (1-normal(abs(rank_event)))*2
gen pvalue_all = (1-normal(abs(rank_all)))*2

sort eventyear event_time

collapse meanK_all sdK_all, by(event_time)

saveold  Table_6_1, replace



